import {createStore} from "vuex";
import {get, post} from "@/utils/request";

const store = createStore({
    state: {
        device: 'desktop',
        isLoadMenu: false,
        menus: [],
        //开启左侧菜单
        openSlide: true,
        withoutAnimation: true,
        fontSize: localStorage.getItem('size') || 'default',
        visitedViews: [],
        cachedViews: [],
        routers: [],
        avatar: '123456',
        permissionList: {}
    },
    getters: {
        isLoadMenu (state) {
            return state.isLoadMenu
        },
        menus: state => state.menus,
        device: state=>state.device,
        openSlide: state => state.openSlide,
        avatar: state=> state.avatar
    },
    mutations: {
       setUser(state,{isLoad,avatar,roles}){
          state.isLoadMenu=isLoad;
          state.avatar=avatar
          state.permissionList=roles
       },
       setLoadMenu(state,isLoad){
           state.isLoadMenu=isLoad
       },
       setMenus(state,menus){
           state.menus=menus
       },
        setDevice(state,device){
            state.device=device
        },
        setOpenSlide(state,{openSlide,withoutAnimation}){
            state.openSlide=openSlide
            state.withoutAnimation=withoutAnimation
        },
        setSize(state,size){
           localStorage.setItem('size',size)
            state.fontSize=size
        }
    },
    actions:{

        getUserInfo({commit}){
          return new Promise((resolve, reject)=>{
              get('/auth/info').then(res=>{
                  commit('setUser',{isLoad: true,avatar: res.avatarName,roles:res.roles})

                  resolve(res)
              })
          })
        },
        setMenus({commit},menus){
            commit('setMenus',menus)
        },
        setDevice({commit},device){
            commit('setDevice',device)
            commit('setSlide',device==='mobile')
        },
        setOpenSlide({commit},obj){
            commit('setOpenSlide',obj)
        },
        setSize({commit},size){
            commit('setSize',size)
        },
        addView({commit},view) {
            commit('addVisitedView',view)
            commit('addCachedView',view)
        },
        addVisitedView({commit},view) {
            console.log('添加',view,view.name,view.meta,view.meta.title)
            if (this.state.visitedViews.some(v => v.path === view.path)) return
            this.state.visitedViews.push(
                Object.assign({}, view, {
                    title: view.meta.title || 'no-name'
                })
            )
        },
        delVisitedView({commit},view){
            return new Promise((resolve=>{
                for (const [i, v] of this.state.visitedViews.entries()) {
                    if (v.path === view.path) {
                        this.state.visitedViews.splice(i, 1)
                        break
                    }
                }
                console.log('xians',[...this.state.visitedViews])
                resolve({
                    visitedViews:[...this.state.visitedViews]
                })
            }))
        },
        delOthersVisitedViews({commit},view) {
            return new Promise(resolve => {
                console.log('viessssss',this.state.visitedViews,view.path)
                this.state.visitedViews = this.state.visitedViews.filter(v => {
                    console.log('比较',v.path,view.path)
                    return v.meta.affix || v.path === view.path
                })
                console.log('viess',this.state.visitedViews)
                resolve({visitedViews:[...this.state.visitedViews]})
            })
        },
        delAllVisitedViews({commit},view) {
            return new Promise(resolve => {
                this.state.visitedViews = this.state.visitedViews.filter(tag => tag.meta.affix)
                resolve({visitedViews:[...this.state.visitedViews]})
            })
        },
        delRightTags({commit},view) {
            return new Promise(resolve => {
                const index = this.state.visitedViews.findIndex(v => v.path === view.path)
                if (index === -1) {
                    return
                }
                this.state.visitedViews = this.state.visitedViews.filter((item, idx) => {
                    return idx <= index || (item.meta && item.meta.affix);

                })
                resolve([...this.state.visitedViews])
            })
        },
        delLeftTags({commit},view) {
            return new Promise(resolve => {
                const index = this.state.visitedViews.findIndex(v => v.path === view.path)
                if (index === -1) {
                    return
                }
                this.state.visitedViews = this.state.visitedViews.filter((item, idx) => {
                    return idx >= index || (item.meta && item.meta.affix);
                })
                resolve([...this.state.visitedViews])
            })
        },
        addCachedView(view) {
            if (this.cachedViews.includes(view.name)) return
            if (!view.meta.noCache) {
                this.cachedViews.push(view.name)
            }
        },
        logout(){
            post('/auth/logout').then(res=>{
                location.reload()
            })
        }
    }
})

export  default  store
